<template>
  <!-- 公司设置 -->
  <div>
    <!-- 搜索 -->
    <TopFilter
      :list="topFilterInit"
      :cols="3"
      labelWidth="100"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :isSelectColumn="false"
      :params="flterValue"
      :datakey="'records'"
      :defaultSelections="selectionList"
      :onRowSelectChange="rows => this.selectionList = rows"
      columnsRef='origin_personnelInfo_postSet_index_01'
      :onColumnsChange="columns => this.columns = columns"
      height="auto"
    >
      <template slot="controls" >
        <multiuse-button size="small" type="primary" :click="claimExportData">数据导出</multiuse-button>
<!--        <el-button size="small" type="primary" icon="el-icon-plus" @click="dialogHandle('','add')">新增</el-button>-->
      </template>
    </FilterTable>
    <!-- 经销商弹窗 -->
    <Drawer
      :visible.sync="visible"
      title="公司信息"
      destroyOnClose
      :closable=false
      :containerStyle="{width:'70vw', minWidth:'1000px', height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <Detail  v-model="detail" :rowData="rowData" :type="type" ref='addDialog' @close="dialogClose" />
    </Drawer>
    <!--<BaseDialog title="公司信息维护" :visible.sync="visible"-->
                <!--width="00px"-->
                <!--destroyOnClose-->
                <!--:containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}" >-->
      <!--<Detail  v-model="initData" ref='addDialog' @close="dialogClose" />-->
    <!--</BaseDialog>-->
  </div>
</template>
<script>
  import { pageDealerStationMaintaion,issueStation } from '@/api/cms-api/personnelInfo/postSet/index';
  import { getDealerCompanyByPage,getClaimListForExport,updateResetPasswords } from '@/api/common/company/index.js';
  import { dictionary } from '@/utils/dictMixin';
  import { region } from '@/api/cms-common/region';//行政区域
  import { DICT_CODE } from '@/api/cms-api/origin/commonDealerType';
  import { getComOrgList } from '@/api/common/org/companyOrgManage/index.js';
  import Detail from '@/pages/cms-pages/origin/originInfo/company/component/detail.vue';
  import { exportExcelFile } from '@/utils/index';
  export default {
    name:'company',
    components: {
      Detail
    },
    mixins: [dictionary, region],
    data() {
      return {
        visible:false,
        isSelectColumn:true,
        infoId:null,
        detail: {},
        topFilterInit:this.topFilterInitFun(),
        flterValue:{
          validStatus:10011001,
          companyType:15061003,
          dealerType:17211002,
        },//IsinitParam: 1 },  //参数,默认页面为启用状态IsinitParam:1
        initData:{},
        rowData:{},
        fetchParams: {}, //ipa 接口
        type:"",
        columns:this.creatHeader(),
        fetchApi:getDealerCompanyByPage,
        selectionList:[],
        DATA_TYPE_DEALER:15061003,
        exportFlag: false
      };
    },
    mounted() {
      this.BaseTable = this.$refs.table.$refs.pageTable;
      this.initProvince();
    },
    methods: {
      //搜索数据
      topFilterInitFun(){
        return [
          { type: 'INPUT',   label: '公司代码',    fieldName: 'companyCode',  placeholder: '请输入',  initialValue: ''},
          { type: 'INPUT',   label: '公司名称',    fieldName: 'companyNameCn',  placeholder: '请输入',  initialValue: ''},
          { type: 'INPUT',   label: '公司简称',    fieldName: 'companyShortNameCn',  placeholder: '请输入',  initialValue: ''},
          { type: 'SELECT', label: '省份', fieldName: 'provinceId', placeholder: '请选择', initialValue: '', itemList: [],change: props => this.initCity(props)},
          { type: 'SELECT', label: '城市', fieldName: 'cityId', placeholder: '请选择', initialValue: '', itemList: [], change: props => this.initTown(props)},
          { type: 'SELECT', label: '县区', fieldName: 'districtId', placeholder: '请选择', initialValue: '', itemList: [] },
          { type: 'SELECT',  label: '有效状态',  fieldName: 'validStatus',  placeholder: '请选择', initialValue: 10011001, itemList:  this.createDictList('1001') },
          { type: 'SELECT',  label: '客户类型',  fieldName: 'dealerType',  placeholder: '请选择', initialValue: 17211002, itemList:  this.createDictList('1721') },
        ]
      },
      //搜索获取数据
      changeHandle(val){
        this.flterValue = Object.assign({companyType:this.DATA_TYPE_DEALER,validStatus:10011001,dealerType:17211002},val);
      },
      //table表头和数据源
      creatHeader(){
        return [
          {
            title: '序号',
            dataIndex: 'index',
            width: 80,
            sorter: true,
            render: props => {
              return <span>{props.row.index + 1}</span>;
            }
          },
          {
            title: '操作',
            dataIndex: 'column-action',
            width: 200,
            fixed: 'left',
            render: (props, h) => {
              return (
                <div>
                <el-button size="mini" type="text" onClick={() => this.dialogHandle(props.row,'edit')}>编辑</el-button>
                  <el-button size="mini" type="text" onClick={() => this.dialogHandle(props.row,'show')}>查看</el-button>
                 <el-button size="mini" disabled={props.row.dealerType == 17211002?false:true}  type="text" onClick={() => this.resetPasswords(props.row,'resetPasswords')}>重置管理员密码</el-button>
              </div>
            );
            }
          },
          { title: '公司代码',  dataIndex: 'companyCode', sorter: true ,filter:true ,filterType:'input'},
          { title: '公司名称',  dataIndex: 'companyNameCn', sorter: true,filter:true,filterType:'input'},
          { title: '公司简称',  dataIndex: 'companyShortNameCn', sorter: true,filter:true,filterType:'input'},
          { title: '公司名称（英文）',  dataIndex: 'companyNameEn', sorter: true,filter:true,filterType:'input'},
          { title: '公司地址',  dataIndex: 'addressCn', sorter: true,filter:true,filterType:'input'},
          { title: '法人',  dataIndex: 'representative', sorter: true,filter:true,filterType:'input'},
          { title: '法人联系电话',  dataIndex: 'representativeTel', sorter: true,filter:true,filterType:'input'},
          { title: '法人邮箱',  dataIndex: 'representativeEmail', sorter: true,filter:true,filterType:'input'},
          { title: '注册资金（万元）',  dataIndex: 'registeredCapital', sorter: true,filter:true,filterType:'input'},
          { title: '统一社会信用代码',  dataIndex: 'unifiedSocialCreditCode', sorter: true,filter:true,filterType:'input'},
          { title: '成立时间',  dataIndex: 'establishmentDate', sorter: true,filter:true,filterType:'input',dateFormat:'yyyy-MM-dd'},
          { title: '注册地址',  dataIndex: 'registerAddress', sorter: true,filter:true,filterType:'input'},
          { title: '邮编',  dataIndex: 'zipCode', sorter: true,filter:true,filterType:'input'},
          { title: '公-司Email',  dataIndex: 'eMail', sorter: true,filter:true,filterType:'input'},
          { title: '省份',  dataIndex: 'provinceName', sorter: true,filter:true,filterType:'input'},
          { title: '城市',  dataIndex: 'cityName', sorter: true,filter:true,filterType:'input'},
          { title: '区县',  dataIndex: 'districtName', sorter: true,filter:true,filterType:'input'},
          { title: '有效状态',  dataIndex: 'validStatus', sorter: true,filterItems: this.createDictList('1001'),
            render: props => {    //编码转汉字
              return <span>{this.createDictText(props.row.validStatus, '1001')}</span>;
            }
            ,filter:true
            ,filterType:'checkbox'
          },
              // { title: '最后下发时间',dataIndex: 'lastIssueTime', dateFormat: 'yyyy-MM-dd HH:mm:ss',  sorter: true},
            ]
      },
      // 公司信息维护导出
      async claimExportData() {
        await getClaimListForExport(this.flterValue ).then(res => {
          console.log(res);

          exportExcelFile(res.data, '公司信息维护导出');
        });
      },
      //初始化所在省份
      async initProvince(){
        let pl = await this.createRegionList();
        this.topFilterInit.find(item => item.fieldName === 'provinceId').itemList = pl;
      },
      //初始化所在市数据
      async initCity(provinceId){
        if(!provinceId || provinceId==null || typeof(provinceId)=='undefined'){
          return;
        }
        //取省数据
        let rd = this.getRegionData();
        this.cityData = rd[provinceId].children;
        let cl = this.createSubRegionList(this.cityData);
        //初始化市，清除原值
        let city = this.topFilterInit.find(item => item.fieldName === 'cityId');
        city.itemList = cl;
        city.initialValue = '';
        //清除区县
        let district = this.topFilterInit.find(item => item.fieldName === 'districtId');
        district.itemList = [];
        district.initialValue = '';
      },
      //初始化所在县市数据
      async initTown(cityId){
        if(!cityId || cityId==null || typeof(cityId)=='undefined'){
          return;
        }
        let td = this.cityData[cityId].children;
        let tl = this.createSubRegionList(td);
        //初始化区县，清除原值
        let district = this.topFilterInit.find(item => item.fieldName === 'districtId');
        district.itemList = tl;
        district.initialValue = '';
      },
       // 重置密码
      resetPasswords(row,type) {
        this.$confirm('是否继续重置?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          updateResetPasswords(row.companyCode).then(res=>{
            if(res.returnCode == 200){
                this.$message({
                  type: 'success',
                  message: '重置成功!'
                });
                // 重新加载
                this.fetchParams = { ...this.fetchParams };
            }else{
              this.$message({
                  type: '400',
                  message: '重置失败!'
                });
            }
          })

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消重置'
          });
        });
      },
      //详情
      dialogHandle(row,type){
        this.visible = true;
        const pram = {
          type: type,
          rowData: row

        };
        this.initData=pram;
        this.rowData = row;
        this.type = type;

      },
      //下发
      async issueHandle(){
        let rows = this.selectionList;
        if(rows.length == 0){
          this.$message.warning("至少选择一条记录");
          return;
        }
        let ids = new Array();
        rows.forEach(row => {
          ids.push(row.stationId);
        });
        const res = await issueStation(ids.join(","))
        // 刷新列表数据
        this.flterValue = { ...this.flterValue };
      },
      //关闭详情
      dialogClose(type){
        this.visible=false
        if (type === 'refresh') {
          // 刷新列表数据
          this.flterValue = { ...this.flterValue };
        }
      },
      collapseHandle() {
        this.$nextTick(() => {
          this.BaseTable.EXECUTE_RESET_HEIGHT();
        });
      }
    }
  };
</script>

<style lang="less" scoped>

</style>
